package Persistence;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import Domain.Deck;
import Domain.IDomainObject;
import Util.ErrorHelper;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

/**
 * Repository class for communicating with Deck table in database.
 * */
public class DeckRepository{
	
	private Context _Context;
	private DatabaseHelper _DBManager;
	private final String _DECK_TABLE = "tblDecks";
	private final String _DECK_COL_ID = "intID";
	private final String _DECK_COL_NAME = "strName";
	
	/**
	 * Default constructor. Initializes the database manager instance then 
	 * creates the database and opens the connection.
	 * @param  context	The application context
	 * */
	public DeckRepository(Context context){
		_Context = context;
		_DBManager = new DatabaseHelper(_Context);
		try {
			_DBManager.createDataBase();
			_DBManager.openDataBase();
		} catch (IOException e) {
			ErrorHelper.reportError(this.getClass(), _Context, e);
		}
	}
	
	/**
	 * Adds a deck object to the database.
	 * @param	deckToAdd	The deck instance to add.
	 * @throws Exception 
	 * */
	public Deck addRow(Deck deck) throws Exception {
		ContentValues values = new ContentValues();
		values.put(_DECK_COL_NAME, deck.getName());
		SQLiteDatabase db = _DBManager.getWritableDatabase();
		try {
			int rowID = (int)db.insert(_DECK_TABLE, null, values);
			deck = getDeck(rowID);
		} catch(Exception e) {
			ErrorHelper.reportError(this.getClass(), _Context, e);
		}
		db.close();
		return deck;
	}

	public void deleteRow(IDomainObject domainObject) {
		// TODO Auto-generated method stub
		
	}

	/**
	 * Gets all decks from the database.
	 * @return	{@link List<Deck>}
	 * @throws	Exception
	 * */
	public List<Deck> getAllRows() throws Exception {
		List<Deck> deckList = new ArrayList<Deck>();
	    String selectQuery = "SELECT  * FROM " + _DECK_TABLE;
	    SQLiteDatabase db = _DBManager.getWritableDatabase();
	    
	    try {
		    Cursor cursor = db.rawQuery(selectQuery, null);
		    if (cursor.moveToFirst()) {
		        do {
		            Deck deck = new Deck(cursor.getInt(1));
		            deck.setName(cursor.getString(0));
		            deckList.add(deck);
		        } while (cursor.moveToNext());
		    }
	    } catch(Exception e) {
	    	ErrorHelper.reportError(this.getClass(), _Context, e);
	    }
	    
	    db.close();
	    return deckList;
	}
	
	/**
	 * Gets a single deck from the database matching the given ID.
	 * @param	int		The ID of the deck to retrieve.
	 * @return	{@link Deck}	
	 * @throws Exception 
	 * */
	public Deck getDeck(int rowID) throws Exception {
		String selectQuery = "SELECT * FROM " + _DECK_TABLE + 
				" WHERE " + _DECK_COL_ID + " = " + rowID;
		SQLiteDatabase db = _DBManager.getReadableDatabase();
		
		try {
			Cursor cursor = db.rawQuery(selectQuery, null);
			if (cursor.moveToFirst()){
				Deck deck = new Deck(cursor.getInt(1));
	            deck.setName(cursor.getString(0));
	            return deck;
			}
		} catch (Exception e) {
			ErrorHelper.reportError(this.getClass(), _Context, e);
		}
		
		db.close();
		return null;
	}

	public void updateRow(Deck domainObject) {
		// TODO Auto-generated method stub
		
	}

}
